if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})

function ready() {
    var user = null, html = [], thisPage = null;
    var walletRechargePage = function () {
        this.init();
    };
    walletRechargePage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if(!user){
              redirect("../account/login.html");
              return;
            }
            $.ajax({
            	type:"GET",
            	url: CONFIG_MANAGER.RequstUrl + "v1/member/asset/cash/" + user.id,
            	async:true,
            	headers:{ token:user.token }
            }).done(function(data){
              $("#cashAmount").html(data.cash);
            }).fail(function(data){
              
            }).always(function(data){ 
              
            });
            
        },
        bindEvent: function () {
            //后退
            $("#back").on("click", function () {
                back();
            });
            //清除输入
            $("#clear").on("click", function () {
                $("input").val("");
            });
            $("#tramount,#paypsd").on("input", function () {
                if ("" == $("#tramount").val() ||
                    "" == $("#paypsd").val()) {
                    $("#rechargeNow").addClass("gray_color").unbind();
                } else {
                    $("#rechargeNow").removeClass("gray_color").unbind().on("click", function () {
                        if( ! is_positiveinteger($("#tramount").val()) ){
                          $.toast("充值金额请输入正整数");
                          return;
                        }
                        if( parseInt($("#cashAmount").html()) <= 0 ){
                          $.toast("现金金额为0");
                          return;
                        }
                        if(parseFloat($("#tramount").val()) > parseInt($("#cashAmount").html())){
                          $.toast("现金金额不足");
                          return;
                        }
                        var requestData = {
                          member_id:user.id,
                          paypsd:$("#paypsd").val(),
                          tramount:$("#tramount").val(),
                        };
                        $.ajax({
                        	type:"POST",
                        	url: CONFIG_MANAGER.RequstUrl + "v1/member/asset/cash/transfer",
                        	async:true,
                        	headers:{ token:user.token },
                        	data:requestData,
                        }).done(function(data){
	                        $.alert("充值成功", function () {
                            	if(AGENT_CLIENT.APP){
									plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
									plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(4)");
                            	}else{
                            		redirect("../mine/mylife.html");
                            	}
	                            return false;
	                        });
                        }).fail(function(data){
	                        var infor = data.responseJSON.Message;
	                        $.toast(infor);
                        });
                        
                        
                        
                    });
                }


            });


        }
    };
    thisPage = new walletRechargePage();
}
